<?php
/**
 * Created by PhpStorm.
 * User: xutao
 * Date: 2018/8/1
 * Time: 17:01
 */
namespace app\index\controller;
use app\admin\model\MemberModel;
use think\Controller;
use think\Loader;
use think\Session;

class Login extends Controller{
    //登录
    public function login(){
        if(request()->isPost()){
            $account = input('account');
            $pwd = input('pwd');
            if(empty($account) || empty($pwd)) return json(['code'=>2,'data'=>'','msg'=>'参数为空']);
            //验证用户是否存在
            $re = MemberModel::login_verify($account,$pwd);
            if(!$re) return json(['code'=>1,'data'=>$re,'msg'=>'账号或密码错误']);
            Session::set('member',$re);
            return json(['code'=>200,'data'=>$re,'msg'=>'ok']);
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //注册接口
    public function register(){
        if(request()->isPost()){
            $data = input();
            if(empty($data['verfiy_code']) || ($data['verfiy_code'] != Session::get('verify_code'))) return json(['code'=>8,'data'=>'','msg'=>'验证码错误']);
            //验证
            $member_validate = Loader::validate('MemberValidate');
            if(!$member_validate->check($data)) return json(['code'=>2,'data'=>'','msg'=>$member_validate->getError()]);
            $re = MemberModel::create_data($data);
            if($re){
                return json(['code'=>200,'data'=>['id'=>$re],'msg'=>'注册成功']);
            }else{
                return json(['code'=>1,'data'=>'','msg'=>'注册失败']);
            }
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //验证码
    public function verfiy_code(){
        if(request()->isPost()){
            if(!input('phone')) return json(['code'=>1,'data'=>'','msg'=>'电话不能为空']);
            $num = str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
            //存入session
            Session::set('verify_code',$num);
            return json(['code'=>200,'data'=>['code'=>$num],'msg'=>'ok']);
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
    //登录状态接口
    public function login_state(){
        if (!Session::get('member')) return json(['code' => 9, 'data' => '', 'msg' => '请登录']);
        return json(['code'=>200,'data'=>Session::get('member'),'msg'=>'ok']);
    }
    //忘记密码
    public function wj_pwd(){
        if(request()->isPost()){
            $tel = input('tel');
            $verify_code = input('verify_code');
            $pwd = input('pwd');
            if(empty($tel) || empty($verify_code) || empty($pwd)) return json(['code'=>2,'data'=>'','msg'=>'参数错误']);
            //查询账号是否存在
            $re = MemberModel::where('tel',$tel)->find();
            //查询账号是否存在
            if(!$re) return json(['code'=>0,'data'=>'','msg'=>'改手机号不存在']);
            if($verify_code != Session::get('verify_code')) return json(['code'=>1,'data'=>'','msg'=>'验证码错误']);
            $member = MemberModel::where('tel',$tel)->save(['pwd'=>$pwd]);
            if(!$member) return json(['code'=>3,'data'=>'','msg'=>'修改失败']);
            return json(['code'=>200,'data'=>'','msg'=>'修改成功']);
        }else{
            return json(['code'=>0,'data'=>'','msg'=>'非法请求']);
        }
    }
}